package com.song.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.baomidou.mybatisplus.extension.api.R;
import com.song.commonutils.ResultObj;
import com.song.servicebase.exceptionhandler.GuliException;
import com.song.vod.service.VodService;
import com.song.vod.utils.ConstantVodUtils;
import com.song.vod.utils.InitVodCilent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author song
 * @data 2020/4/1
 */
@RequestMapping("/eduvod/video")
@CrossOrigin
@RestController
@Api(description = "视频管理")
public class VodController {

    @Autowired
    private VodService vodService;


    /**
     * 上传视频
     * @param file
     * @return
     */
    @ApiOperation(value = "上传视频")
    @PostMapping("/uploadAlyiVideo")
    public String uploadAlyiVideo(MultipartFile file){

        String videoId = vodService.uploadVideoAly(file);
        return videoId;

    }

    /**
     * 删除阿里云视频
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResultObj deleteAlyVideo(@PathVariable String id){
        int result=vodService.deleteAlyVideo(id);
        if(result==1){
            return ResultObj.DELETE_SUCCESS;
        }
        return ResultObj.DELETE_ERROR;
    }

    /**
     * 删除多条阿里云视频
     * @param videoIdList
     * @return
     */
    @DeleteMapping("/deleteBatch")
    public void deleteBatchVideo(@RequestParam("videoIdList")  List<String> videoIdList){
        vodService.deleteBatchVideo(videoIdList);
    }

    //根据视频id获取视频凭证
    @GetMapping("getPlayAuth/{id}")
    public ResultObj getPlayAuth(@PathVariable String id) {
        try {
            //创建初始化对象
            DefaultAcsClient client =
                    InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return ResultObj.ok().data("playAuth",playAuth);
        }catch(Exception e) {
            return ResultObj.falseObj("获取凭证失败");
        }
    }
}
